package com.shuma.auth.service.task;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shuma.auth.entity.PRODUCTSTATE;
import com.shuma.auth.mapper.ProductBaseMapper;
import com.shuma.auth.model.po.ProductBase;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Component
public class OnLineOrOffLineTask {

    @Resource
    ProductBaseMapper productBaseMapper;

    @Scheduled(cron = "0 * * * * ? ") //每分钟触发一次
    public void onLineProduct(){
        //查询所有可以上线的数据
        LambdaQueryWrapper<ProductBase> productBaseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productBaseLambdaQueryWrapper.eq(ProductBase::getState, PRODUCTSTATE.APPROVED)
                .lt(ProductBase::getTimeOn, LocalDateTime.now());
        List<ProductBase> productBases = productBaseMapper.selectList(productBaseLambdaQueryWrapper);
        //修改状态
        for (ProductBase productBase : productBases) {
            productBase.setState(PRODUCTSTATE.ONLINE);
            productBaseMapper.updateById(productBase);
        }
    }



    @Scheduled(cron = "0 * * * * ? ") //每分钟触发一次
    public void offLineProduct(){
        //查询所有可以下线的数据
        LambdaQueryWrapper<ProductBase> productBaseLambdaQueryWrapper = new LambdaQueryWrapper<>();
        productBaseLambdaQueryWrapper.eq(ProductBase::getState, PRODUCTSTATE.ONLINE)
                .lt(ProductBase::getTimeOff, LocalDateTime.now());
        List<ProductBase> productBases = productBaseMapper.selectList(productBaseLambdaQueryWrapper);
        //修改状态
        for (ProductBase productBase : productBases) {
            productBase.setState(PRODUCTSTATE.OFFLINE);
            productBaseMapper.updateById(productBase);
        }
    }
}
